var jizhu = false
function remember(){
    jizhu = !jizhu
}
(function(){
    document.getElementById('error').style.opacity = '0'
    if(sessionStorage.getItem('merchantNo') && sessionStorage.getItem('username') && sessionStorage.getItem('password')){
        // console.log(sessionStorage.getItem('merchantNo'))
        document.getElementsByTagName('input')[0].value = sessionStorage.getItem('merchantNo')
        document.getElementsByTagName('input')[1].value = sessionStorage.getItem('username')
        document.getElementsByTagName('input')[2].value = sessionStorage.getItem('password')
    }
    
    document.getElementById('login').onclick = function(){
        login();
    }
    document.getElementById('forget').onclick = function(){
        window.location.href = 'static/forget.html'
    }
    
    function login(){
        var formObj = {}
        formObj.merchantNo = parseInt(document.getElementsByTagName('input')[0].value)
        formObj.username = document.getElementsByTagName('input')[1].value
        formObj.password = hex_md5(document.getElementsByTagName('input')[2].value);
        $.ajax({
            // url:"http://10.17.1.82:8001/api/user/login",
            url: initObj.url + "/api/user/login",
            type:'post',
            contentType:'application/json',
            data:JSON.stringify(formObj),
            datatype:'json',
            success:function(res){
                if(res.status == 300){
                    document.getElementById('error').style.opacity = "1"
                    return 
                }
                sessionStorage.setItem('firstLogin',"")
                console.log(res.data.firstLogin)
                if(res.data.firstLogin){
                    console.log(1)
                    sessionStorage.setItem('firstLogin',true)
                }
                console.log(res)
                sessionStorage.setItem('token', res.data.token)
                if(jizhu){
                    sessionStorage.setItem('merchantNo',formObj.merchantNo)
                    sessionStorage.setItem('username',formObj.username)
                    sessionStorage.setItem('password',document.getElementsByTagName('input')[2].value)
                }
                if(res.data.role == '管理员'){
                    window.location.href = 'main/shouye-admin/shouye-admin.html'
                }else{
                    window.location.href = 'main/shouye/shouye.html'
                }
            },
            // error:function(){
            //     document.getElementById('error').style.display   = "block"
            // }
        })
    }
})()